package com.example.firstdemo.ui.screens.pages.addProblem.addProblem.selectProblem

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.IntrinsicSize
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.text.input.rememberTextFieldState
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.Send
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.Icon
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.LocalTextStyle
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.firstdemo.network.api.addProblem.data.ProblemDictData
import com.example.firstdemo.ui.screens.components.SubPage
import com.example.firstdemo.ui.screens.pages.addProblem.addProblem.selectProblem.data.LocalSelectProblemData
import com.example.firstdemo.ui.screens.pages.addProblem.addProblem.selectProblem.data.SelectProblemUIState
import com.example.firstdemo.ui.theme.AppTheme
import com.example.firstdemo.ui.theme.Space
import com.example.firstdemo.ui.theme.eColors
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.distinctUntilChanged


@OptIn(FlowPreview::class)
@Composable
fun SelectProblemScreen(
    padding: PaddingValues = PaddingValues(0.dp),
    uiState: SelectProblemUIState,
    onBack: () -> Unit = {},
    onGetProblemList: () -> Unit = {},
    onFilterProblem: (str: String) -> Unit = {},
    onSelectProblem: (ProblemDictData) -> Unit = { _ -> }
) {
    val problemList = uiState.problemList
    val textFieldState = rememberTextFieldState()
    LaunchedEffect(Unit) {
        onGetProblemList()
        snapshotFlow { textFieldState.text }
            .debounce(300) // 防抖 300ms
            .distinctUntilChanged()
            .collect { text ->
                onFilterProblem(text.toString())
            }
    }
    SubPage(
        padding = padding,
        title = "新增故障(故障选择)",
        onBack = onBack
    ) {
        Column(
            Modifier
                .fillMaxSize()
                .padding(Space.medium)
        ) {
            TextField(
                state = textFieldState,
                colors = TextFieldDefaults.colors(
                    unfocusedContainerColor = MaterialTheme.eColors.background.base,
                    focusedIndicatorColor = Color.Transparent,
                    unfocusedIndicatorColor = Color.Transparent
                ),
                shape = MaterialTheme.shapes.medium,
                modifier = Modifier.fillMaxWidth(),
                prefix = {
                    Icon(
                        Icons.Filled.Search,
                        contentDescription = null,
                        tint = MaterialTheme.eColors.text.placeholder
                    )
                },
            )
            Spacer(Modifier.height(Space.small))
            LazyColumn(
                verticalArrangement = Arrangement.spacedBy(Space.small),
                modifier = Modifier.fillMaxSize()
            ) {
                items(problemList.size) { index ->
                    DataItem(
                        problem = problemList[index],
                        modifier = Modifier.clickable(onClick = {
                            onSelectProblem(problemList[index])
                        })
                    )
                }
            }
        }
    }
}

@Composable
fun DataItem(problem: ProblemDictData, modifier: Modifier = Modifier) {
    Surface(shape = MaterialTheme.shapes.small, modifier = modifier) {
        CompositionLocalProvider(
            LocalTextStyle provides LocalTextStyle.current.copy(color = MaterialTheme.eColors.text.placeholder),
            LocalContentColor provides MaterialTheme.eColors.text.placeholder
        ) {
            Row(
                Modifier
                    .fillMaxWidth()
                    .height(IntrinsicSize.Min)
                    .background(MaterialTheme.eColors.basic.white)
                    .padding(Space.medium, Space.small)
            ) {
                Box(
                    contentAlignment = Alignment.Center,
                    modifier = Modifier.fillMaxHeight()
                ) {
                    Icon(
                        Icons.Filled.Search,
                        contentDescription = null,
                    )
                }
                Spacer(Modifier.width(Space.small))
                Box(Modifier.weight(1f)) {
                    Column {

                    }
                    Text(
                        text = "${problem.label}[${problem.value}]",
                        maxLines = 1,
                        overflow = TextOverflow.Ellipsis
                    )
                }
                Spacer(Modifier.width(Space.small))
                Box(
                    contentAlignment = Alignment.Center,
                    modifier = Modifier.fillMaxHeight()
                ) {
                    Icon(
                        Icons.AutoMirrored.Filled.Send,
                        contentDescription = null,
                        Modifier.size(20.dp)
                    )
                }
            }
        }
    }
}

@Preview()
@Composable
fun DataItemPreview() {
    AppTheme {
        DataItem(LocalSelectProblemData.problem)
    }
}

@Preview(
    showSystemUi = true
)
@Composable
fun SelectionProblemPreview() {
    AppTheme {
        SelectProblemScreen(
            padding = PaddingValues(top = 30.dp),
            uiState = LocalSelectProblemData.uiState
        )
    }
}